home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / ohlfind.zip / MODECHAN.C < prev    next >
C/C++ Source or Header  |  1990-05-31  |  10KB  |  327 lines

  1. /* modechange.c -- file mode manipulation
  2.    Copyright (C) 1989, 1990 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Written by David MacKenzie <djm@ai.mit.edu> */
  19.  
  20. /* The ASCII mode string is compiled into a linked list of `struct
  21.    modechange', which can then be applied to each file to be changed.
  22.    We do this instead of re-parsing the ASCII string for each file
  23.    because the compiled form requires less computation to use; when
  24.    changing the mode of many files, this probably results in a
  25.    performance gain. */
  26.  
  27. #include <sys/types.h>
  28. #include <sys/stat.h>
  29. #include "modechange.h"
  30.  
  31. #ifdef STDC_HEADERS
  32. #include <stdlib.h>
  33. #else
  34. char *malloc ();
  35.  
  36. #ifndef NULL
  37. #define NULL 0
  38. #endif
  39. #endif
  40.  
  41. /* Return newly allocated memory to hold one element of type TYPE. */
  42. #define talloc(type) ((type *) malloc (sizeof (type)))
  43.  
  44. #define isodigit(c) ((c) >= '0' && (c) <= '7')
  45.  
  46. static int oatoi ();
  47.  
  48. /* Return a linked list of file mode change operations created from
  49.    MODE_STRING, an ASCII string that contains either an octal number
  50.    specifying an absolute mode, or symbolic mode change operations with
  51.    the form:
  52.    [ugoa...][[+-=][rwxXstugo...]...][,...]
  53.    MASKED_OPS is a bitmask indicating which symbolic mode operators (=+-)
  54.    should not affect bits set in the umask when no users are given.
  55.    Operators not selected in MASKED_OPS ignore the umask.
  56.  
  57.    Return MODE_INVALID if `mode_string' does not contain a valid
  58.    representation of file mode change operations;
  59.    return MODE_MEMORY_EXHAUSTED if there is insufficient memory. */
  60.  
  61. struct mode_change *
  62. mode_compile (mode_string, masked_ops)
  63.      register char *mode_string;
  64.      unsigned masked_ops;
  65. {
  66.   struct mode_change *head;    /* First element of the linked list. */
  67.   struct mode_change *change;    /* An element of the linked list. */
  68.   int i;            /* General purpose temporary. */
  69.   int umask_value;        /* The umask value (surprise). */
  70.   unsigned short affected_bits;    /* Which bits in the mode are operated on. */
  71.   unsigned short affected_masked; /* `affected_bits' modified by umask. */
  72.   unsigned ops_to_mask;        /* Operators to actually use umask on. */
  73.  
  74.   i = oatoi (mode_string);
  75.   if (i >= 0)
  76.     {
  77.       if (i > 07777)
  78.     return MODE_INVALID;
  79.       head = talloc (struct mode_change);
  80.       if (head == NULL)
  81.     return MODE_MEMORY_EXHAUSTED;
  82.       head->next = NULL;
  83.       head->op = '=';
  84.       head->flags = 0;
  85.       head->value = i;
  86.       head->affected = 07777;    /* Affect all permissions. */
  87.       return head;
  88.     }
  89.  
  90.   umask_value = umask (0);
  91.   umask (umask_value);        /* Restore the old value. */
  92.  
  93.   head = NULL;
  94.   --mode_string;
  95.  
  96.   /* One loop iteration for each "ugoa...=+-rwxXstugo...[=+-rwxXstugo...]". */
  97.   do
  98.     {
  99.       affected_bits = 0;
  100.       ops_to_mask = 0;
  101.       /* Turn on all the bits in `affected_bits' for each group given. */
  102.       for (++mode_string;; ++mode_string)
  103.     switch (*mode_string)
  104.       {
  105.       case 'u':
  106.         affected_bits |= 04700;
  107.         break;
  108.       case 'g':
  109.         affected_bits |= 02070;
  110.         break;
  111.       case 'o':
  112.         affected_bits |= 01007;
  113.         break;
  114.       case 'a':
  115.         affected_bits |= 07777;
  116.         break;
  117.       default:
  118.         goto no_more_affected;
  119.       }
  120.  
  121.     no_more_affected:
  122.       /* If none specified, affect all bits, except perhaps those
  123.      set in the umask. */
  124.       if (affected_bits == 0)
  125.     {
  126.       affected_bits = 07777;
  127.       ops_to_mask = masked_ops;
  128.     }
  129.  
  130.       while (*mode_string == '=' || *mode_string == '+' || *mode_string == '-')
  131.     {
  132.       /* Add the element to the tail of the list, so the operations
  133.          are performed in the correct order. */
  134.       if (head == NULL)
  135.         {
  136.           head = talloc (struct mode_change);
  137.           if (head == NULL)
  138.         return MODE_MEMORY_EXHAUSTED;
  139.           change = head;
  140.         }
  141.       else
  142.         {
  143.           change->next = talloc (struct mode_change);
  144.           if (change->next == NULL)
  145.         {
  146.           mode_free (change);
  147.           return MODE_MEMORY_EXHAUSTED;
  148.         }
  149.           change = change->next;
  150.         }
  151.  
  152.       change->next = NULL;
  153.       change->op = *mode_string;    /* One of "=+-". */
  154.       affected_masked = affected_bits;
  155.       if (ops_to_mask & (*mode_string == '=' ? MODE_MASK_EQUALS
  156.                  : *mode_string == '+' ? MODE_MASK_PLUS
  157.                  : MODE_MASK_MINUS))
  158.         affected_masked &= ~umask_value;
  159.       change->affected = affected_masked;
  160.       change->value = 0;
  161.       change->flags = 0;
  162.  
  163.       /* Set `value' according to the bits set in `affected_masked'. */
  164.       for (++mode_string;; ++mode_string)
  165.         switch (*mode_string)
  166.           {
  167.           case 'r':
  168.         change->value |= 00444 & affected_masked;
  169.         break;
  170.           case 'w':
  171.         change->value |= 00222 & affected_masked;
  172.         break;
  173.           case 'X':
  174.         change->flags |= MODE_X_IF_ANY_X;
  175.         /* Fall through. */
  176.           case 'x':
  177.         change->value |= 00111 & affected_masked;
  178.         break;
  179.           case 's':
  180.         /* Set the setuid/gid bits if `u' or `g' is selected. */
  181.         change->value |= 06000 & affected_masked;
  182.         break;
  183.           case 't':
  184.         /* Set the "save text image" bit if `o' is selected. */
  185.         change->value |= 01000 & affected_masked;
  186.         break;
  187.           case 'u':
  188.         /* Set the affected bits to the value of the `u' bits
  189.            on the same file.  */
  190.         if (change->value)
  191.           goto invalid;
  192.         change->value = 00700;
  193.         change->flags |= MODE_COPY_EXISTING;
  194.         break;
  195.           case 'g':
  196.         /* Set the affected bits to the value of the `g' bits
  197.            on the same file.  */
  198.         if (change->value)
  199.           goto invalid;
  200.         change->value = 00070;
  201.         change->flags |= MODE_COPY_EXISTING;
  202.         break;
  203.           case 'o':
  204.         /* Set the affected bits to the value of the `o' bits
  205.            on the same file.  */
  206.         if (change->value)
  207.           goto invalid;
  208.         change->value = 00007;
  209.         change->flags |= MODE_COPY_EXISTING;
  210.         break;
  211.           default:
  212.         goto no_more_values;
  213.           }
  214.     no_more_values:;
  215.     }
  216.   } while (*mode_string == ',');
  217.   if (*mode_string == 0)
  218.     return head;
  219. invalid:
  220.   mode_free (head);
  221.   return MODE_INVALID;
  222. }
  223.  
  224. /* Return file mode OLDMODE, adjusted as indicated by the list of change
  225.    operations CHANGES.  If OLDMODE has the S_IFDIR bit set, the type `X'
  226.    change affects it even if no execute bits were set in OLDMODE.
  227.    The returned value has the S_IFMT bits cleared. */
  228.  
  229. unsigned short
  230. mode_adjust (oldmode, changes)
  231.      unsigned oldmode;
  232.      register struct mode_change *changes;
  233. {
  234.   unsigned short newmode;    /* The adjusted mode and one operand. */
  235.   unsigned short value;        /* The other operand. */
  236.  
  237.   newmode = oldmode & 07777;
  238.  
  239.   for (; changes; changes = changes->next)
  240.     {
  241.       if (changes->flags & MODE_COPY_EXISTING)
  242.     {
  243.       /* Isolate in `value' the bits in `newmode' to copy, given in
  244.          the mask `changes->value'. */
  245.       value = newmode & changes->value;
  246.  
  247.       if (changes->value & 00700)
  248.         /* Copy `u' permissions onto `g' and `o'. */
  249.         value |= (value >> 3) | (value >> 6);
  250.       else if (changes->value & 00070)
  251.         /* Copy `g' permissions onto `u' and `o'. */
  252.         value |= (value << 3) | (value >> 3);
  253.       else
  254.         /* Copy `o' permissions onto `u' and `g'. */
  255.         value |= (value << 3) | (value << 6);
  256.  
  257.       /* In order to change only `u', `g', or `o' permissions,
  258.          or some combination thereof, clear unselected bits.
  259.          This can not be done in mode_compile because the value
  260.          to which the `changes->affected' mask is applied depends
  261.          on the old mode of each file. */
  262.       value &= changes->affected;
  263.     }
  264.       else
  265.     {
  266.       value = changes->value;
  267.       /* If `X', do not affect the execute bits if the file is not a
  268.          directory and no execute bits are already set. */
  269.       if ((changes->flags & MODE_X_IF_ANY_X)
  270.           && (oldmode & S_IFMT) != S_IFDIR
  271.           && (newmode & 00111) == 0)
  272.         value &= ~00111;    /* Clear the execute bits. */
  273.     }
  274.  
  275.       switch (changes->op)
  276.     {
  277.     case '=':
  278.       /* Preserve the previous values in `newmode' of bits that are
  279.          not affected by this change operation. */
  280.       newmode = (newmode & ~changes->affected) | value;
  281.       break;
  282.     case '+':
  283.       newmode |= value;
  284.       break;
  285.     case '-':
  286.       newmode &= ~value;
  287.       break;
  288.     }
  289.     }
  290.   return newmode;
  291. }
  292.  
  293. /* Free the memory used by the list of file mode change operations
  294.    CHANGES. */
  295.  
  296. void
  297. mode_free (changes)
  298.      register struct mode_change *changes;
  299. {
  300.   register struct mode_change *next;
  301.  
  302.   while (changes)
  303.     {
  304.       next = changes->next;
  305.       free (changes);
  306.       changes = next;
  307.     }
  308. }
  309.  
  310. /* Return a positive integer containing the value of the ASCII
  311.    octal number S.  If S is not an octal number, return -1.  */
  312.  
  313. static int
  314. oatoi (s)
  315.      char *s;
  316. {
  317.   register int i;
  318.  
  319.   if (*s == 0)
  320.     return -1;
  321.   for (i = 0; isodigit (*s); ++s)
  322.     i = i * 8 + *s - '0';
  323.   if (*s)
  324.     return -1;
  325.   return i;
  326. }
  327.